<?php
namespace App\Admin\Controllers\Common;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class ReportBase extends Controller {
	protected $term = 0;
	protected $start_date = 0;
	protected $start_timestamp = 0;
	public function __construct($year,$season) {
		$this->start_date = DB::table("terms")
			->where([
				'year' => $year,
				'season' => $season
			])->orderBy('id','desc')
			->value("start_date")." 00:00:00";
		$this->start_timestamp = strtotime($this->start_date);
		$this->term = (int)ceil((time() - $this->start_timestamp)/3600/24/7);
	}

	public function getProtectedAttr() {
		return [
			"week" => $this->term,
			"start_date" => $this->start_date,
			"start_timestamp" => $this->start_timestamp
		];
	}

	/**
	 *	[根据传入的起止周返回对应的起始时间]
	 * @param int $start_week 起始周
	 * @param int $end_week 结束周
	 * @return array
	 */
	public function getTimebyWeek($start_week = 1,$end_week = 1) {
		$s_date = date("Y-m-d",$this->start_timestamp + ($start_week - 1) * 3600 * 24 * 7);
		$e_date = date("Y-m-d",$this->start_timestamp + ($end_week - 1) * 3600 * 24 * 7);
		return [
			's_date' => $s_date,
			'e_date' => $e_date,
			's_timestamp' => strtotime($s_date),
			'e_timestamp' => strtotime($e_date)
		];
	}

	/**
	 * [判断设备]
	 * @return bool
	 */
	public function is_mobile() {
		if(isset($_SERVER['HTTP_X_WAP_PROFILE'])){return true;}
		if(isset($_SERVER['HTTP_VIA'])){
			return stristr($_SERVER['HTTP_VIA'],"wap")? true : false;
		}
		if(isset($_SERVER['HTTP_USER_AGENT'])){
			$clientkeywords = [
				'mobile','nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap'
			];
			if(preg_match("/(".implode('|',$clientkeywords).")/i",strtolower($_SERVER['HTTP_USER_AGENT']))){
				return true;
			}
		}
		if(isset($_SERVER['HTTP_ACCEPT'])){
			if((strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')!==false)&&(strpos($_SERVER['HTTP_ACCEPT'],'text/html')===false||(strpos($_SERVER['HTTP_ACCEPT'],'vnd.wap.wml')<strpos($_SERVER['HTTP_ACCEPT'],'text/html')))){
				return true;
			}
		}
		return false;
	}
}
